package com.hdu.clj.dncard

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.hdu.clj.dncard.databaseUtil.HeroDatabaseUtil
import com.hdu.clj.dncard.util.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_map1.*

class MainActivity : AppCompatActivity() {

    private var zxMapList = ArrayList<ZxMap>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val acc = LoadAndSave().load("userAcc",this)
        val heroAtt = HeroDatabaseUtil().search(this,1,acc)
        val heroName = LoadAndSave().load("name",this)
        val cur_hid = HeroDatabaseUtil().findHero(this,acc)
        if (cur_hid==1){
            mainhero.setImageResource(R.drawable.hero0)
        }
        else{
            mainhero.setImageResource(R.drawable.hero1)
        }
        var hattack = 0
        var hdefence = 0
        var hwisdom = 0
        var hlife = 0
        val lv = findLv().find(this,acc)
        val zbname = HeroDatabaseUtil().search_equipname(this,acc)
        zb1name.setText(zbname[0])
        zb2name.setText(zbname[1])
        zb3name.setText(zbname[2])
        zb4name.setText(zbname[3])
        zb5name.setText(zbname[4])
        zb1attack.setText("攻击+"+heroAtt[4])
        zb1wisdom.setText("智力+"+heroAtt[5])
        zb1life.setText("生命+"+heroAtt[6])
        zb2attack.setText("攻击+"+heroAtt[7])
        zb2wisdom.setText("智力+"+heroAtt[8])
        zb2life.setText("生命+"+heroAtt[9])
        zb3attack.setText("攻击+"+heroAtt[10])
        zb3wisdom.setText("智力+"+heroAtt[11])
        zb3life.setText("生命+"+heroAtt[12])
        zb4attack.setText("攻击+"+heroAtt[13])
        zb4wisdom.setText("智力+"+heroAtt[14])
        zb4life.setText("生命+"+heroAtt[15])
        zb5attack.setText("攻击+"+heroAtt[16])
        zb5wisdom.setText("智力+"+heroAtt[17])
        zb5life.setText("生命+"+heroAtt[18])
        hattack = heroAtt[0]+5*lv
        hdefence = heroAtt[1]
        hwisdom = heroAtt[2]+5*lv
        hlife = heroAtt[3]+10*lv

        heroAttr.setText("角色名：$heroName\n等级：Lv-$lv\n攻击力：$hattack\n防御力：$hdefence\n智力：$hwisdom\n生命值：$hlife")
        LoadAndSave().save(hattack.toString(),"hattack",this)
        LoadAndSave().save(hdefence.toString(),"hdefence",this)
        LoadAndSave().save(hwisdom.toString(),"hwisdom",this)
        LoadAndSave().save(hlife.toString(),"hlife",this)
        LoadAndSave().save(hlife.toString(),"maxlife",this)

        zxMapList = InitMaps().init(this)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        mapRecycler.layoutManager = layoutManager
        val adapter = MapAdapter(zxMapList,this)
        mapRecycler.adapter = adapter

        zb.setOnClickListener {
            drawerLayout.openDrawer(GravityCompat.START)
        }

    }

    override fun onResume() {
        super.onResume()
        val acc = LoadAndSave().load("userAcc",this)
        val heroAtt = HeroDatabaseUtil().search(this,1,acc)
        val heroName = LoadAndSave().load("name",this)
        var hattack = 0
        var hdefence = 0
        var hwisdom = 0
        var hlife = 0
        val lv = findLv().find(this,acc)
        val zbname = HeroDatabaseUtil().search_equipname(this,acc)
        zb1name.setText(zbname[0])
        zb2name.setText(zbname[1])
        zb3name.setText(zbname[2])
        zb4name.setText(zbname[3])
        zb5name.setText(zbname[4])
        zb1attack.setText("攻击+"+heroAtt[4])
        zb1wisdom.setText("智力+"+heroAtt[5])
        zb1life.setText("生命+"+heroAtt[6])
        zb2attack.setText("攻击+"+heroAtt[7])
        zb2wisdom.setText("智力+"+heroAtt[8])
        zb2life.setText("生命+"+heroAtt[9])
        zb3attack.setText("攻击+"+heroAtt[10])
        zb3wisdom.setText("智力+"+heroAtt[11])
        zb3life.setText("生命+"+heroAtt[12])
        zb4attack.setText("攻击+"+heroAtt[13])
        zb4wisdom.setText("智力+"+heroAtt[14])
        zb4life.setText("生命+"+heroAtt[15])
        zb5attack.setText("攻击+"+heroAtt[16])
        zb5wisdom.setText("智力+"+heroAtt[17])
        zb5life.setText("生命+"+heroAtt[18])
        hattack = heroAtt[0]+5*lv
        hdefence = heroAtt[1]
        hwisdom = heroAtt[2]+5*lv
        hlife = heroAtt[3]+10*lv
        heroAttr.setText("角色名：$heroName\n等级：Lv-$lv\n攻击力：$hattack\n防御力：$hdefence\n智力：$hwisdom\n生命值：$hlife")
        LoadAndSave().save(hattack.toString(),"hattack",this)
        LoadAndSave().save(hdefence.toString(),"hdefence",this)
        LoadAndSave().save(hwisdom.toString(),"hwisdom",this)
        LoadAndSave().save(hlife.toString(),"hlife",this)
        LoadAndSave().save(hlife.toString(),"maxlife",this)
        zxMapList = InitMaps().init(this)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        mapRecycler.layoutManager = layoutManager
        val adapter = MapAdapter(zxMapList,this)
        mapRecycler.adapter = adapter
    }
}
